package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class TestUpdate3 {
    public static void main(String[] args) {
        //Prepardstatement
        Connection con = DBUtil.getConnection();
        PreparedStatement st = null;
        //update subject set =xxx where id=
        Scanner in=new Scanner(System.in);
        System.out.println("请输入妖删除的表名：");
        String tableName=in.nextLine();
        System.out.println("请输入主键：");
        long id=in.nextLong();

        try {
            String sql="delete from "+tableName+" where id=?";

            st=con.prepareStatement(sql);
            st.setString(1,tableName);
            st.setLong(2,id);

            int res = st.executeUpdate();//不需要Sql作为参数
            System.out.printf("成功影响了%d行",res);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(con,st,null);
        }
    }
}
